package com.xxx.userservice.mapper;

import com.xxx.userservice.pojo.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author guwq
 * @since 2025-05-22
 */
public interface UserMapper extends BaseMapper<User> {

    @Select("select money from t_user where userId = #{userId}")
    BigDecimal getMoney(String userId);

    @Update("update t_user set money = money - #{amount}, freeze_amount = freeze_amount + #{amount} where userId = #{userId} and money >= #{amount}")
    boolean freeze(@Param("userId") String userId, @Param("amount") BigDecimal amount);

    @Update("update t_user set freeze_amount = freeze_amount - #{amount} where userId = #{userId}")
    boolean deduct(@Param("userId") String userId, @Param("amount") BigDecimal amount);

    @Update("update t_user set money = money + #{amount}, freeze_amount = freeze_amount - #{amount} where userId = #{userId} and freeze_amount > 0")
    boolean unfreeze(@Param("userId") String userId, @Param("amount") BigDecimal amount);
}
